new Vue({
	el: '#app',
	data: {
		uid: '',
		tel: '',
		password: ''
	},
	mounted: function() {
		this.init();
	},
	methods: {
		init: function() {
			var self = this;
			apiready = function() {
				var fid = api.pageParam;
				self.type = fid.type;
				self.index = fid.index;
				self.name = fid.name;
				self.qid = fid.qid;
				self.aid = fid.aid;
				self.id = fid.id;
				self.cid = fid.cid;
				self.openName = fid.openName;
				api.addEventListener({
					name: 'keyback'
				}, function(ret, err) {
					console.log('按了返回键');
					self.close();
				});
			}
		},
		//关闭登录页并返回原来的页
		close: function() {
			var self = this;
			self.uid = $api.getStorage('uid');
			console.log(self.uid);
			switch(self.type) {
				//有来源页
				case 1:
					if(self.uid) {
						if(self.openName) {
							if(self.qid) {
								api.openWin({
									name: self.openName,
									url: '../community/questionDetails.html',
									reload: true,
									pageParam: {
										qid: self.qid
									}
								});
							} else if(self.aid) {
								api.openWin({
									name: self.openName,
									url: '../community/replyComment.html',
									reload: true,
									pageParam: {
										aid: self.aid
									}
								});
							} else if(self.id) {
								api.openWin({
									name: self.openName,
									url: '../community/commentDialogue.html',
									reload: true,
									pageParam: {
										id: self.id
									}
								});
							} else if(self.cid) {
								api.openWin({
									name: self.openName,
									url: '../mall/productMsg.html',
									reload: true,
									pageParam: {
										id: self.cid
									}
								});
							}
						}
					} else {
						api.closeWin();
					}
					break;
					//没有来源页
				case 2:
					api.closeWin({
						name: 'user-setting'
					});
					if(self.uid) {
						if(self.index) {
							api.openWin({
								name: 'index',
								url: '../../index.html',
								reload: true,
								pageParam: {
									index: self.index
								}
							});
						}
					} else {
						api.openWin({
							name: 'index',
							url: '../../index.html',
							reload: true,
							pageParam: {
								index: 2
							}
						});
					}
					break;
				default:
					break;
			}
			api.closeWin();
		},
		//登录
		login: function() {
			var self = this;
			var data = {
				mobile: self.tel,
				password: self.password
			};
			$.post(base + config.guest_login, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					$api.setStorage('uid', msg.uid);
					$api.setStorage('url', msg.url);
					$api.setStorage('avatar', msg.avatar);
					$api.setStorage('avatar_path', msg.avatar_path);
					$api.setStorage('email', msg.email);
					$api.setStorage('nickname', msg.nickname);
					$api.setStorage('role', msg.role);
					$api.setStorage('username', msg.username);
					$api.setStorage('api_token', msg.api_token);
					self.tel = "";
					self.password = "";
					self.close();
				} else {
					api.toast({
						msg: msg.msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//跳转到注册页
		jump: function() {
			api.openFrame({
				name: 'register',
				url: 'register.html',
				reload: true,
				rect: {
					x: 0,
					y: 0,
					w: 'auto',
					h: 'auto',
				}
			});
		},
		//跳转到忘记密码页面
		forger: function() {
			api.openFrame({
				name: 'forget',
				url: 'forgerPassword.html',
				reload: true,
				rect: {
					x: 0,
					y: 0,
					w: 'auto',
					h: 'auto',
				}
			});
		}
	}
})